<?php

class Site extends Model_Abstract
{
	public function getAllSites()
	{
		try
		{
			$sql = 'select * from sites';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function add($name, $domain, $game, $jackpot, $jackpot_percent, $template, $mon1, $mon2, $mon3, $mon4, $topAd, $rightAd, $bottomAd,
							$p5, $p10, $p15, $p20, $p25, $p30, $p35, $p40, $p45, $p50, $locale, $keywords, $description)
	{
		try
		{
			$sql = 'insert into sites set name=?, domain=?, game=?, jackpot=?, jackpot_percent=?, template=?, monetization1=?, monetization2=?, monetization3=?, 
					monetization4=?, ad_top=?, ad_right=?, ad_bottom=?, p5=?, p10=?, p15=?, p20=?, p25=?, p30=?, p35=?, p40=?, p45=?, p50=?, 
					locale=?, keywords=?, description=?';
			$this->_connection->execute($sql, array($name, $domain, $game, $jackpot, $jackpot_percent, $template, $mon1, $mon2, $mon3, $mon4, $topAd, $rightAd, $bottomAd,
							$p5, $p10, $p15, $p20, $p25, $p30, $p35, $p40, $p45, $p50, $locale, $keywords, $description));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getSiteById($id)
	{
		try
		{
			$sql = 'select * from sites where id=? limit 1';
			$result = $this->_connection->query($sql, array($id));
			if (count($result) == 0)
			{
				return false;
			}
			else
			{
				return $result[0];
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function update($id, $name, $domain, $game, $jackpot, $jackpot_percent, $template, $mon1, $mon2, $mon3, $mon4, $topAd, $rightAd, $bottomAd,
							$p5, $p10, $p15, $p20, $p25, $p30, $p35, $p40, $p45, $p50, $locale, $keywords, $description)
	{
		try
		{
			$sql = 'update sites set name=?, domain=?, game=?, jackpot=?, jackpot_percent=?, template=?, monetization1=?, monetization2=?, monetization3=?, 
					monetization4=?, ad_top=?, ad_right=?, ad_bottom=?, p5=?, p10=?, p15=?, p20=?, p25=?, p30=?, p35=?, p40=?, p45=?, p50=?, 
					locale=?, keywords=?, description=? where id=?';
			$this->_connection->execute($sql, array($name, $domain, $game, $jackpot, $jackpot_percent, $template, $mon1, $mon2, $mon3, $mon4, $topAd, $rightAd, $bottomAd,
							$p5, $p10, $p15, $p20, $p25, $p30, $p35, $p40, $p45, $p50, $locale, $keywords, $description, $id));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function deleteSite($id)
	{
		try
		{
			$sql = 'delete from sites where id=?';
			$this->_connection->execute($sql, array($id));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getAllSitesAsList()
	{
		try
		{
			$sql = 'select id, domain from sites';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function addToPaid($amount , $id){
		$sql = 'update sites set paid=paid+? where id=?';
		try
		{
			return $this->_connection->execute($sql, array($amount, $id));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
}
?>